<template>
  <div class="dialog">
    <el-dialog :title="'饲料' + titleText" :visible.sync="showDialog" width="45%"  :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm"  :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="饲料名称" prop="materialId">
          <el-select class="w100p" filterable clearable v-model="formData.materialId"  placeholder="请选择">
            <el-option v-for="(item, index) of materialList" :key="index + 'area'" :label="item.name"
              :value="item.materialId" />
          </el-select>
        </el-form-item>
        <el-form-item label="关联品种" prop="varietyId">
          <el-select class="w100p" filterable clearable v-model="formData.varietyId" placeholder="请选择">
            <el-option v-for="(item, index) of varietyList" :key="index + 'area'" :label="item.varietyName"
              :value="item.fishVarietyId" />
          </el-select>
        </el-form-item>
        <el-form-item label="饲料转化比" prop="feedRatio">
          <el-input v-model="formData.feedRatio" type="number" min="0" clearable placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FodderApi from "@/api/basic-info-manage/fodder-management";
import MaterialApi from "@/api/basic-info-manage/material";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety";
export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    const validateNumber = (rule, value, callback)=> {
      if (value < 0) {
        callback(new Error('不可输入负数'));
      }else{
        callback();
      }
    }
    return {
      formData: {},
      materialList: [],
      varietyList: [],
      rules: {
        materialId: [
          { required: true, message: "请选择饲料", trigger: ["change", "blur"] }
        ],
        varietyId: [
          { required: true, message: "请选择鱼品种", trigger: ["change", "blur"] }
        ],
        feedRatio: [
          { required: true, message: "请输入饲料转化比", trigger: ["change", "blur"] },
          { validator: validateNumber, trigger: 'blur' }
        ]
      },
      showDialog: this.show

    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
      if(v){
        this.queryMaterial()
        this.queryVarietyApi()
      }
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    queryMaterial() {
      MaterialApi.queryByCondition({ materialTypeNo: "feed" }).then(res => {
        this.materialList = res.data
      })
    },
    queryVarietyApi() {
      FishVarietyApi.queryByCondition({ materialTypeNo: "feed" }).then(res => {
        this.varietyList = res.data
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            FodderApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            FodderApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
